package ui.activity.poscontrol;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import base.BaseAct;
import base.BaseBiz;
import butterknife.BindView;
import event.HasPushEvent;
import event.NetErrorEvent;
import ui.activity.poscontrol.adapter.HasPushPeopleAdapter;
import ui.activity.poscontrol.beanmodel.PosMainBean;
import ui.activity.poscontrol.beanmodel.PushPeopleBean;
import ui.activity.poscontrol.beanmodel.PushPosBean;
import ui.activity.poscontrol.biz.PosControlMainBiz;
import ui.activity.poscontrol.component.DaggerPosControlMainComponent;
import ui.activity.poscontrol.contract.PosControlMainContract;
import ui.activity.poscontrol.module.PosControlMainModule;
import ui.activity.poscontrol.presenter.PosControlMainPresenter;


/**
 * 已划拨终端列表
 * Created by chenjian on 18/2/22.
 */

public class HasPushAct extends BaseAct implements PosControlMainContract.View {

    @Inject
    PosControlMainPresenter presenter;
    @Inject
    PosControlMainBiz biz;


    @BindView(R.id.posrecord_txt_total)
    TextView posrecord_txt_total;
    @BindView(R.id.posrecord_txt_jh)
    TextView posrecord_txt_jh;

    @BindView(R.id.pushpeople_list)
    RecyclerView pushpeople_list;

    private List<PushPeopleBean> userInfoData = new ArrayList<>();
    HasPushPeopleAdapter adapter;
    @Override
    protected void setRoot() {
        setContentView(R.layout.activity_haspush);
    }

    @Override
    protected void setup() {
        DaggerPosControlMainComponent.builder().posControlMainModule(new PosControlMainModule(this)).build().inject(this);
        setToolBar("已划拨终端列表", "");
        pushpeople_list.setLayoutManager(new LinearLayoutManager(HasPushAct.this));
        presenter.setBiz(biz);
        biz.queryHasPushPeopleData(TobuyApplication.getCustomId(),new BaseBiz.Callback<List<PushPeopleBean>>() {
            @Override
            public void onSuccess(List<PushPeopleBean> posMainBean) {
                userInfoData.addAll(posMainBean);
                int totalsize = 0;
                for (int i = 0;i<posMainBean.size();i++){
                    totalsize+=posMainBean.get(i).getCount();

                }
                posrecord_txt_total.setText("终端数量："+totalsize+"台");
                if(null == adapter){
                    adapter= new HasPushPeopleAdapter(HasPushAct.this,posMainBean);
                    pushpeople_list.setAdapter(adapter);
                }else{
                    adapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onFailure(List<PushPeopleBean> getUrlOutput) {

            }

        });

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void registerCancelOrder(HasPushEvent event) {
        biz.queryHasPushPosData(TobuyApplication.getCustomId(),event.toId,1,10000,new BaseBiz.Callback<PushPosBean>() {
            @Override
            public void onSuccess(PushPosBean pushPosBean) {
                        for ( int i = 0;i<userInfoData.size();i++){
                                if(pushPosBean.getRecords().get(0).getToId()==userInfoData.get(i).getToId()){
                                    userInfoData.get(i).getPosRecords().addAll(pushPosBean.getRecords());
                                    break;
                                }

                        }
                adapter.setData(userInfoData);

            }

            @Override
            public void onFailure(PushPosBean pushPosBean) {

            }

        });
    }
    @Override
    protected void init(@Nullable Bundle savedInstanceState) {


    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }




    @Override
    public void upDateUI(PosMainBean bean) {
//        my_pos_num.setText(bean.getPosCount()+"台");
//        posrecord_txt_total.setText("终端数量："+(bean.getPosCount()+bean.getFlowCount())+"台");
//        posopen_txt_total.setText("已开通："+bean.getOpenedCount()+"台");
//        posrecord_txt_jh.setText("已激活"+bean.getActiveCount()+"台");
//        push_pos_num.setText(bean.getFlowCount()+"台");
    }

    @Override
    public void showPopWindow() {
    }

    @Override
    public void showFlowRuleDialog(String items) {
    }
}
